package com.stadsetjari_hi;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;

import com.stadsetjari_hi.Fragments.Loggin_Fragment;
import com.stadsetjari_hi.Fragments.Registration_Fragment;
import com.stadsetjari_hi.Fragments.Settings_Fragment;
import com.stadsetjari_hi.Fragments.Transmit_Location_Fragment;
import com.stadsetjari_hi.transmissionclasses.Stadsetjari;

//http://www.java2s.com/Code/Android/Core-Class/Demonstrationofhidingandshowingfragments.htm
public class Main extends FragmentActivity {
	
	private android.app.FragmentManager fm;
	private SharedPreferences preferences;
	private Context context;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		preferences = getSharedPreferences("my_preferences", MODE_PRIVATE);
		
		setContentView(R.layout.v_mainview);

		if(preferences.getBoolean("savedUserInfo", false))
		{
			if (savedInstanceState != null) return;

			Transmit_Location_Fragment TLFragment = new Transmit_Location_Fragment();
			fm = getFragmentManager();

			android.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
			fragmentTransaction.add(R.id.myfragment, TLFragment);
			
			fragmentTransaction.commit();	
		}
		else
		{
			if (findViewById(R.id.myfragment) != null) {
	
				if (savedInstanceState != null) {
					return;
				}		
				
				Registration_Fragment regFragment = new Registration_Fragment();
				
				regFragment.setArguments(getIntent().getExtras());
				
				fm = getFragmentManager();
				
				android.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
				fragmentTransaction.add(R.id.myfragment, regFragment);
				
				fragmentTransaction.commit();	
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) 
		{
			case android.R.id.home:
				NavUtils.navigateUpFromSameTask(this);
				return true;
				
			case R.id.Loggin:
			{
				if(!preferences.getBoolean("savedUserInfo", false))
				{
					if (findViewById(R.id.Loggin_user_fragment)== null)
					{
						
						Loggin_Fragment logFragment = new Loggin_Fragment();
						logFragment.setArguments(getIntent().getExtras());
						
						android.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
						fragmentTransaction.setCustomAnimations(R.animator.fade_in, R.animator.fade_out);
						fragmentTransaction.replace(R.id.myfragment, logFragment);
						fragmentTransaction.commit();	
						return true;
					}
					return true;
				}
				if(preferences.getBoolean("savedUserInfo", true))
				{
	                Transmit_Location_Fragment TLFragment = new Transmit_Location_Fragment();
	                TLFragment.setArguments(getIntent().getExtras());
					
					android.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
					fragmentTransaction.setCustomAnimations(R.animator.fade_in, R.animator.fade_out);
					fragmentTransaction.replace(R.id.myfragment, TLFragment);
					fragmentTransaction.commit();
					return true;
				}
				return true;
			}
			case R.id.Register: 
			{
  				Stadsetjari.LogOutUser(context);
				if (findViewById(R.id.Register_user_fragment)== null)
				{
				Registration_Fragment regFragment = new Registration_Fragment();
				regFragment.setArguments(getIntent().getExtras());
				
				android.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
				fragmentTransaction.setCustomAnimations(R.animator.fade_in, R.animator.fade_out);
				fragmentTransaction.replace(R.id.myfragment, regFragment);
				fragmentTransaction.commit();
				return true;
				}
				else return true;
			}
			case R.id.action_settings: 
			{
				if (findViewById(R.id.Settings_user_fragment)== null)
				{
					Settings_Fragment setFragment = new Settings_Fragment();
					setFragment.setArguments(getIntent().getExtras());

					android.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
					fragmentTransaction.setCustomAnimations(R.animator.fade_in, R.animator.fade_out);
					fragmentTransaction.replace(R.id.myfragment, setFragment);
					fragmentTransaction.commit();
					return true;
				}
				else return true;
			}
		
		}
		return super.onOptionsItemSelected(item);
	}
}
